using System.Collections.Generic;
using UnityEngine;
using XNode;

namespace JufGame
{
	[System.Serializable]
	public class LOD_Feature : Node
	{
		[SerializeField, Input(ShowBackingValue.Never)] private FeatureEnum lastLOD;
		public LOD_Config[] Configs => configs;
        public FeatureEnum E_Feature;
		[SerializeField] private int baseAudacity = 1;
		
		[SerializeField, Output(ShowBackingValue.Always, ConnectionType.Multiple, dynamicPortList = true)] 
		private LOD_Config[] configs;

		[SerializeField, Input(dynamicPortList = true)]
		private AI_LOD[] constraints;

		public void InitConfigs()
		{
			for(int i = 0; i < configs.Length; ++i)
			{
				configs[i].SetAudacityVecotr(baseAudacity);
			}
		}

		/// <summary>
		/// 根据输出口下标，获取对应输出口所连接的子节点
		/// </summary>
		/// <param name="configIndex"></param>
		/// <returns>与特定输出点相连的所有子节点</returns>
		public List<LOD_Feature> GetChildFeaturesByCfgIdx(int configIndex)
		{
			var connects = GetOutputPort("configs " + configIndex.ToString()).GetConnections();
			var children = new List<LOD_Feature>();
			foreach(var c in connects)
			{
				if(c.fieldName == "lastLOD")
				{
					children.Add(c.node as LOD_Feature);
				}
			}
			return children;
		}
		/// <summary>
		/// 获取该LOD的约束
		/// </summary>
		/// <param name="configIndex"></param>
		/// <returns>所需满足的特定特征的约束</returns>
		public Dictionary<LOD_Feature, AI_LOD> GetConstraintsByCfgIdx(int configIndex)
		{
			var connects = GetOutputPort("configs " + configIndex.ToString()).GetConnections();
			var children = new Dictionary<LOD_Feature, AI_LOD>();
			foreach(var c in connects)
			{
				if(c.node != this && c.fieldName.Contains("constraints"))
				{
					var feature = c.node as LOD_Feature;
					children[feature] = (AI_LOD)c.node.GetValue(c);
				}
			}
			return children;
		}
		public override object GetValue(NodePort port)
		{
			if (port.fieldName == "lastLOD")//直接连接节点
			{
				return E_Feature;
			}
			else if(port.fieldName.Contains("configs"))//输出LOD
			{
				return port.node.WithDynamicListPort(port, "configs", i => configs[i]);
			}
			else//约束LOD
			{
				return port.node.WithDynamicListPort(port, "constraints", i => constraints[i]);
			}
		}
    }
}
